(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:ActivityCtrl
     * @description
     * # ActivityCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('ActivityCtrl', ActivityCtrl);

    ActivityCtrl.$inject = ['$scope', 'dataService', '$state', 'helper', '$location', '$rootScope'];

    function ActivityCtrl($scope, dataService, $state, helper, $location, $rootScope) {

        $scope.listAction = function() {

            $scope.getlist();
            $scope.$on('event:up-store-id', function() {
                $scope.getlist();
            });
        }

        //查询活动列表
        $scope.getlist = function(pageNo) {
            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize,
                query: { store_id: $rootScope.store_id }
            };

            dataService.activitypageGet(query).then(function(r) {
                if (!r.Msg) {
                    $scope.activitys = r.activity_pages;
                    $scope.activity_Total = r.total_count;
                }
            })
        }

        $scope.delete = function(id) {
            if (id) {
                helper.myConfirm("确认删除此文章吗", function() {
                    var data = { 'activity_page_id': id };
                    dataService.activitypageDel(data).then(function(result) {
                        if (result.activity_page_id) {
                            // $scope.getList(1);
                            helper.myAlert("success", null, null, function() {
                                $scope.getlist();
                            });
                        } else {
                            helper.myAlert("error:" + result);
                        }
                    });
                });
            }
        }

        $scope.addAction = function() {

            $scope.activity_id = $location.search().a_id;
            $scope.acid = random();
            if ($scope.activity_id) {
                dataService.activitypageOne($scope.activity_id).then(function(r) {
                    if (r) {
                        $scope.activity = r;
                    }
                })
            }
        }

        $scope.saveData = function() {

            if(!$rootScope.store_id){
                helper.myAlert("店铺id没有找到:请重新授权");
                return ;
            }
            
            $scope.activity.store_id = $rootScope.store_id;

            var Data = clone($scope.activity);

            Data.content = JSON.stringify(html2json(Data.content));

            var fun = $scope.activity_id ? "activitypageUpdate" : "activitypageAdd";

            dataService[fun](Data).then(function(r) {
                if (r.Msg) {
                    helper.myAlert("error:" + r.Msg);
                } else {
                    helper.myAlert("success", null, null, function() {
                        $state.go("activity.list");
                    });
                }
            })
        }
    }
})();